<?php
# Global configuration file, created on 2011-03-26 02:03:16 265 for microblog.bimal.org.np
# Remove this file, if it does NOT have anything configured here.
# Every configurarations will be available publicly to the entities under this subdomain.

$logged_on = $variable->session('logged_on', 'integer', 0); # It will be the time() of last successful login.
$user_id   = $variable->session('user_id',   'integer', 0); # Who has logged in currently? 0 = No user.

# Conmfirm session variables
$variable->write('session', 'is_admin', $variable->session('is_admin', 'boolean', false));
$variable->write('session', 'user_id', $variable->session('user_id', 'integer', 0));

# Proceed only if the page requires a login
if($page_details['needs_login']!='N')
{
	# Search for login, if done already.
	# $logged_on!==true, if boolean flag used. Otherwise, time() of last login.
	if(!$user_id || !$logged_on)
	{
		# Jump to the first clicked page that required the login.
		# Commenting the below line will jump to the last clicked page that required the login
		#if(!isset($_SESSION['goto']))
		{
			# We will redirect to $_SESSION['goto'] once the user logs in successfully.
			# Avoid using login.php, logout.php => go to ./ in these cases.
			# Rather go to ./.				
			if(preg_match('/^log.*?$/', $_SERVER['REQUEST_URI'], $data))
			{
				# Do not allow to enter to login/logout pages.
				$_SERVER['REQUEST_URI'] = './';
			}
			
			# Remember the current page to go after successful login
			$_SESSION['goto'] = $_SERVER['REQUEST_URI'];
		}

		# Be sure that it is not having a needs_login=Y flag.
		# Otherwise, it results in a looping redirections.
		stopper::url('login.php'); # Force to redirect to login page
		#$smarty->display('login.php'); die(); # Rather show the login form immediately in the same page
	}
}

# Use this block programatically only, and only if you have implemented
# the logics of controlling admin-login requirements in the database flag.
# You can set the super admin flag (is_admin) while user logs in.
# Avoid the normal users access the admin pages
if($page_details['is_admin'] == 'Y' && !$_SESSION['is_admin'])
{
	stopper::url('permission-denied.php');
}

# category id to category name
function smarty_modifier_category($category_id=0)
{
	$category_id = (int)$category_id;
	$db = new mysql();
	$categroy = $db->row("SELECT category_name FROM microblog_categories WHERE category_id={$category_id};");
	$categroy = isset($categroy['category_name'])?$categroy:array('category_name'=>'');
	return $categroy['category_name'];
}

/**
* Multi purpose re-usage
* If title is absent, try to print the category name
*/
function smarty_modifier_posttitle($title='', $category_id='')
{
	if($title) return $title;
	
	return smarty_modifier_category($category_id);
}
?>